package com.ruoyi.web.controller.app;

import java.util.List;
import java.util.Arrays;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import com.ruoyi.app.domain.bo.AppToUserIdBo;
import com.ruoyi.common.constant.Constants;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.app.domain.vo.AppAssetTransferVo;
import com.ruoyi.app.domain.bo.AppAssetTransferBo;
import com.ruoyi.app.service.IAppAssetTransferService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 转增订单
 *
 * @author app
 * @date 2025-06-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/assetTransfer")
public class AppAssetTransferController extends BaseController {

    private final IAppAssetTransferService iAppAssetTransferService;

    /**
     * 查询转增订单列表
     */
    @SaCheckPermission("system:assetTransfer:list")
    @GetMapping("/list")
    public TableDataInfo<AppAssetTransferVo> list(AppAssetTransferBo bo, PageQuery pageQuery) {
        return iAppAssetTransferService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出转增订单列表
     */
    @SaCheckPermission("system:assetTransfer:export")
    @Log(title = "转增订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppAssetTransferBo bo, HttpServletResponse response) {
        List<AppAssetTransferVo> list = iAppAssetTransferService.queryList(bo);
        ExcelUtil.exportExcel(list, "转增订单", AppAssetTransferVo.class, response);
    }

    /**
     * 获取转增订单详细信息
     *
     * @param transferId 主键
     */
    @SaCheckPermission("system:assetTransfer:query")
    @GetMapping("/{transferId}")
    public R<AppAssetTransferVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long transferId) {
        return R.ok(iAppAssetTransferService.queryById(transferId));
    }

    /**
     * 新增转增订单
     */
    @SaCheckPermission("system:assetTransfer:add")
    @Log(title = "转增订单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppAssetTransferBo bo) {
        return toAjax(iAppAssetTransferService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改转增订单
     */
    @SaCheckPermission("system:assetTransfer:edit")
    @Log(title = "转增订单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppAssetTransferBo bo) {
        return toAjax(iAppAssetTransferService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 申诉处理订单
     */
    @SaCheckPermission("system:assetTransfer:edit")
    @Log(title = "转增订单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/edit")
    public R<Void> userEdit(@RequestBody AppAssetTransferBo bo) {
        if(bo.getTransferId()==null){
            return R.fail("ID不能为空");
        }
        AppAssetTransferVo transferVo = iAppAssetTransferService.queryById(bo.getTransferId());
        if(Objects.isNull(transferVo)){
            return R.fail("订单不存在！");
        }
        if(!StringUtils.equals(transferVo.getAppealStatus(), Constants.FAIL)){
            return R.fail("订单申诉已处理！");
        }
        return R.result( iAppAssetTransferService.customerOrder(transferVo, bo));
    }


    /**
     * 删除转增订单
     *
     * @param transferIds 主键串
     */
    @SaCheckPermission("system:assetTransfer:remove")
    @Log(title = "转增订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{transferIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] transferIds) {
        return toAjax(iAppAssetTransferService.deleteWithValidByIds(Arrays.asList(transferIds), true) ? 1 : 0);
    }


    @SaCheckPermission("system:assetTransfer:remove")
    @Log(title = "冻结用户", businessType = BusinessType.DELETE)
    @PostMapping("/dongjie")
    public R<Void> dongjie(@RequestBody AppToUserIdBo bo) {
        return toAjax(iAppAssetTransferService.dongjie(bo));
    }
}
